package cn.it58.zaitu2.controller;

import cn.it58.zaitu2.dao.DriverRepository;
import cn.it58.zaitu2.entity.Driver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/driver")
public class DriverController {
    @Autowired
    private DriverRepository driverRepository;
    @PostMapping("/all")
    public Iterable<Driver> findall(){
        return driverRepository.findAll();
    }

    @PostMapping("/all2")
    public Iterable<Driver> findall2(@RequestBody Map map){
        Integer p = (Integer) map.get("page");
        Integer size = (Integer) map.get("limit");
        Pageable page = new PageRequest(p-1,size);
        return driverRepository.findAll(page);
    }

    @PostMapping("/add")
    public Driver add(@RequestBody Driver driver){
        return driverRepository.save(driver);
    }
    @PostMapping("/del")
    public boolean del(@RequestBody Driver driver){
        driverRepository.delete(driver);
        return true;
    }
    @PostMapping("/count")
    public long count(){
        return driverRepository.count();
    }
    @PostMapping("/find")
    public Driver find(@RequestBody Driver driver){
        return driverRepository.findById(driver.getId()).get();
    }
}
